package com.fesine.ssh.dao.impl;

import com.fesine.ssh.dao.ProductDao;
import com.fesine.ssh.model.Product;
import com.fesine.ssh.util.GenerateUUID;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Created by Fesine on 2016/2/21.
 */
@Repository("productDao")
public class ProductDaoImpl implements ProductDao {

    @Autowired
    private SessionFactory sessionFactory;

    private Session getCurrentSession() {
        return this.sessionFactory.getCurrentSession();
    }


    @Override
    public Product load(String id) {
        return (Product) this.getCurrentSession().load(Product.class, id);
    }

    @Override
    public Product get(String id) {
        return (Product) this.getCurrentSession().get(Product.class, id);
    }

    @Override
    public String save(Product entity) {
        entity.setId(GenerateUUID.getUUId());
        return (String) this.getCurrentSession().save(entity);
    }

    @Override
    public void update(Product entity) {
        this.getCurrentSession().update(entity);
    }

    @Override
    public Product findByProdId(String prodId) {
        String hql = String.format("from Product where prodId='%s' order by prodId", prodId);
        List<Product> list = this.getCurrentSession()
                .createQuery(hql).setCacheable(true).list();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        } else {
            return list.get(0);
        }
    }

    @Override
    public List<Product> findByKeyword(String keyword) {
        String hql = String.format("from Product");
        if (null != keyword) {
            hql = hql+" where name like '%"+keyword+"%' or descr like '%"+keyword+"%' ";
        }
        hql = hql+" order by prodId ";
        return this.getCurrentSession().createQuery(hql).setCacheable(true).list();
    }

    @Override
    public void deleteByProdId(String prodId) {
        String hql = String.format("delete Product where prodId='%s'", prodId);
        this.getCurrentSession().createQuery(hql).executeUpdate();
    }


    @Override
    public List<Product> findAll() {
        String hql = String.format("from Product order by prodId");
        List<Product> productList =
                this.getCurrentSession().createQuery(hql).setCacheable(true).list();
        return productList;
    }

    @Override
    public List<Product> findAll( int offset, int pageSize) {
        String hql= String.format("from Product order by prodId");
        Query query = this.getCurrentSession().createQuery(hql).setCacheable(true);
        query.setFirstResult(offset);
        query.setMaxResults(pageSize);
        List<Product> productList = query.list();
        return productList;
    }

    @Override
    public List<Product> findByEntity(Product product) {
        String hql = toHql(product);
        List<Product> prodList =
                this.getCurrentSession().createQuery(hql).setCacheable(true).list();
        return prodList;
    }

    @Override
    public List<Product> findByEntity(Product entity, int offset, int pageSize) {
        String hql = toHql(entity);
        Query query = this.getCurrentSession().createQuery(hql).setCacheable(true);
        query.setFirstResult(offset);
        query.setMaxResults(pageSize);
        List<Product> prodList =
                query.list();
        return prodList;
    }

    private String toHql(Product product){
        StringBuffer hql = new StringBuffer("from Product where 1=1 ");
        if (null != product.getProdId()) {
            hql.append("and prodId like '%" + product.getProdId() + "%' ");
        }
        if (null != product.getName()) {
            hql.append("and name like '%" + product.getName() + "%' ");
        }
        if (null != product.getDescr()) {
            hql.append("and descr like '%" + product.getDescr() + "%' ");
        }
        hql.append(" order by prodId");
        return hql.toString();
    }

    @Override
    public void saveOrUpdate(Product entity) {
        this.getCurrentSession().saveOrUpdate(entity);
    }

    @Override
    public void delete(String id) {
        Product entity = this.load(id);
        this.getCurrentSession().delete(entity);
    }

    @Override
    public void flush() {
        this.getCurrentSession().flush();
    }

    @Override
    public void persist(Product entity) {
        this.getCurrentSession().persist(entity);
    }
}
